app.controller('HomeCtrl', function($rootScope, $location)
		{
	$rootScope.activetab = $location.path();
		});

app.controller('SobreCtrl', function($rootScope, $location)
		{
	$rootScope.activetab = $location.path();
		});

app.controller('ContatoCtrl', function($rootScope, $location)
		{
	$rootScope.activetab = $location.path();
		});

app.controller('Usuario', function($scope,$http){
	$scope.usuarios = [
	                   {			                	   			                	   
	                	   firstName:$scope.firstName,
	                	   secondName:$scope.secondName,
	                	   email:$scope.email,
	                	   phoneNumber:$scope.phoneNumber,
	                	   sexo:$scope.sexo
	                   }
	                   ];

	$scope.add = function(){

		var person = 
		{
				_rev:"1-fff118610acc17e1cec8fb2a7e1567c2",
				firstName:$scope.firstName,
				secondName:$scope.secondName,
				email:$scope.email,
				phoneNumber:$scope.phoneNumber,
				sexo:$scope.sexo};

		$http.put("http://192.168.9.30:5984/paulo/82868219",person).success(function(data){
			$scope.usuarios.put(person);	

		});

	};

	$scope.findAll = function(usuario) {

		$http.get("http://192.168.9.30:5984/paulo/82868219").success(function(data) {

			$scope.usuarios.push(data);

		});

	};

	$scope.remover = function(usuario){
		$scope.usuarios = $scope.usuarios.filter(function(item){
			if(usuario._id != item._id){
				return true;
			}
			return false;
		},0);
	};
});



app.controller('OverviewCtrl', ['$scope', 'Machine', function($scope, Machine, $location) {


	
    var promise = Machine.getAll();
    
    promise.then(function(machineList) {
        $scope.machines = machineList;
    }, function(reason) {
        alert('Machine list is empty: ' + reason);
    });
}]);

app.controller('UpdateMachineCtrl', ['$scope', '$routeParams', 'Machine', 
                                            function($scope, $routeParams, Machine) {
    $scope.title = "Update Installation Details";
    var promise = Machine.getSingle($routeParams.docId);

    promise.then(function(machine) {
        $scope.machine = machine;
    }, function(reason) {
        alert('Record could not be retrieved');
    });

    $scope.save = function() {
        Machine.update($scope.machine);
    };
}]);

app.controller('SyncCtrl', ['$scope', 'Machine', function($scope, Machine) {
    $scope.syncDb = function() {
        Machine.sync();
        Machine.checkConflicts();
    };

    $scope.checkCors = function() {
        // Check CORS is supported
        var corsCheck = function(method, url) {
          var xhr = new XMLHttpRequest();

          if ("withCredentials" in xhr) {
            // XHR for Chrome/Firefox/Opera/Safari.
            xhr.open(method, url, true);
          } else if (typeof XDomainRequest != "undefined") {
            // XDomainRequest for IE.
            xhr = new XDomainRequest();
            xhr.open(method, url);
          } else {
            // CORS not supported.
            console.log('CORS not supported by browser');
          }

          xhr.onload = function() {
              console.log('Response from CORS ' + method + ' request to ' + url + ': ' + xhr.responseText);
          };
          xhr.onerror = function() {
              console.log('Error response from CORS ' + method + ' request to ' + url + ': ' + xhr.responseText);
          };

          xhr.send();
        };

        var server = 'http://192.168.9.30:5984/paulo';

        corsCheck('GET', server);
        corsCheck('PUT', server);
        corsCheck('POST', server);
        corsCheck('HEAD', server);
      corsCheck('DELETE', server);
    };
}]);